<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>工作单快速录入</title>
<!-- 导入jquery核心类库 -->
<script type="text/javascript"
	src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
<!-- 导入easyui类库 -->
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath }/js/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath }/js/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath }/js/easyui/ext/portal.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath }/css/default.css">	
<script type="text/javascript"
	src="${pageContext.request.contextPath }/js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath }/js/easyui/ext/jquery.portal.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath }/js/easyui/ext/jquery.cookie.js"></script>
<script
	src="${pageContext.request.contextPath }/js/easyui/locale/easyui-lang-zh_CN.js"
	type="text/javascript"></script>

</head>
<body class="easyui-layout" style="visibility:hidden;">
	<div region="center" border="false">
    	<table id="grid"></table>
	</div>


	<script type="text/javascript">
		var editIndex ;

		function doAdd(){
			if(editIndex != undefined){
				$("#grid").datagrid('endEdit',editIndex);
			}
			if(editIndex==undefined){
				//alert("快速添加电子单...");
				$("#grid").datagrid('insertRow',{
					index : 0,
					row : {}
				});
				$("#grid").datagrid('beginEdit',0);
				editIndex = 0;
			}
		}

		function doSave(){


           if (editIndex!=undefined){

			   $("#grid").datagrid('unselectAll');

			   //选中正在操作的行
			   $("#grid").datagrid('selectRow',editIndex );



			   var datagrid = $("#grid").datagrid('getSelected');

			   $("#grid").datagrid('endEdit',editIndex );

			   $.ajax({
				   url:'${pageContext.request.contextPath}/quick/saveOrUpAllWorkOrderManage.action',
				   type:'POST',
				   contentType:"application/json;charset=UTF-8",
				   data:JSON.stringify(datagrid),
				   dataType:'json',
				   success:function (obj) {

					   if (obj.success && obj.data=="更新"){

						   $.messager.show({
							   title:'消息提示',
							   msg:'更新成功',
							   timeout:1000,
							   showType:'slide'
						   });

						   //清除选中状态
						   $("#grid").datagrid('unselectAll')

					   } else{

						   $.messager.show({
							   title:'消息提示',
							   msg:'添加成功',
							   timeout:1000,
							   showType:'slide'
						   });

						   //清除选中状态
						   $("#grid").datagrid('unselectAll')



					   }

				   }
			   })
		   }

		}


		function doCancel(){
			if(editIndex!=undefined){
				$("#grid").datagrid('cancelEdit',editIndex);
				if($('#grid').datagrid('getRows')[editIndex].id == undefined){
					$("#grid").datagrid('deleteRow',editIndex);
				}
				editIndex = undefined;
			}
		}

		//工具栏
		var toolbar = [ {
			id : 'button-add',
			text : '新增一行',
			iconCls : 'icon-edit',
			handler : doAdd
		}, {
			id : 'button-cancel',
			text : '取消编辑',
			iconCls : 'icon-cancel',
			handler : doCancel
		}, {
			id : 'button-save',
			text : '保存',
			iconCls : 'icon-save',
			handler : doSave
		}];
		// 定义列
		var columns = [ [ {
			field : 'id',
			title : '工作单号',
			width : 300,
			align : 'center',
			editor :{
				type : 'validatebox',
				options : {
					required: true,
					validType:"remote['${pageContext.request.contextPath}/quick/checkIdIsExists.action','id']",
					invalidMessage:'Id存在,请重新输入Id!'
				}
			}
		}, {
			field : 'arrivecity',
			title : '到达地',
			width : 120,
			align : 'center',
			editor :{
				type : 'validatebox',
				options : {
					required: true
				}
			}
		},{
			field : 'product',
			title : '产品',
			width : 120,
			align : 'center',
			editor :{
				type : 'validatebox',
				options : {
					required: true
				}
			}
		}, {
			field : 'num',
			title : '件数',
			width : 120,
			align : 'center',
			editor :{
				type : 'numberbox',
				options : {
					required: true
				}
			}
		}, {
			field : 'weight',
			title : '重量',
			width : 120,
			align : 'center',
			editor :{
				type : 'validatebox',
				options : {
					required: true
				}
			}
		}, {
			field : 'floadreqr',
			title : '配载要求',
			width : 220,
			align : 'center',
			editor :{
				type : 'validatebox',
				options : {
					required: true
				}
			}
		}] ];

		$(function(){
			// 先将body隐藏，再显示，不会出现页面刷新效果
			$("body").css({visibility:"visible"});

			// 收派标准数据表格
			$('#grid').datagrid( {
				iconCls : 'icon-forward',
				fit : true,
				border : true,
				rownumbers : true,
				striped : true,
				pageList: [10,30,50,100],
				pagination : true,
				toolbar : toolbar,
				url:"${pageContext.request.contextPath}/quick/findAllWorkOrderManage.action",   //
				idField : 'id',
				columns : columns,
				onDblClickRow : doDblClickRow,
				onAfterEdit : function(rowIndex, rowData, changes){
					editIndex = undefined;
				}
			});
		});

		function doDblClickRow(rowIndex, rowData){

			//当双击时 结束当前编辑的行
			if(editIndex!=undefined){
				$("#grid").datagrid('cancelEdit',editIndex);
				if($('#grid').datagrid('getRows')[editIndex].id == undefined){
					$("#grid").datagrid('deleteRow',editIndex);
				}
				editIndex = undefined;
			}

			// 开启编辑功能
			$('#grid').datagrid('beginEdit',rowIndex);


			//设置某一列 属性不能被修改   这里设置的是 id 属性
			var datagrid = $("#grid").datagrid('getEditor',{index:rowIndex,field:'id'});

			$(datagrid.target).attr('disabled', true);


			editIndex = rowIndex;
		}
	</script>
</body>
</html>